package com.ostrich.module_community.attention


import android.graphics.Point
import android.os.Build
import android.util.Log
import android.view.View
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.ostrich.library_base.BaseMvvmFragment
import com.ostrich.library_common.utils.DensityUtils
import com.ostrich.library_common.utils.LoggerUtils
import com.ostrich.module_community.R
import com.ostrich.module_community.attention.adapter.AttentionAdapter
import com.ostrich.module_community.attention.entity.AttentionEntity
import com.ostrich.module_community.databinding.CommunityFragmentAttentionBinding
import com.shuyu.gsyvideoplayer.GSYVideoManager
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack
import com.shuyu.gsyvideoplayer.utils.GSYVideoHelper


class AttentionFragment : BaseMvvmFragment<CommunityFragmentAttentionBinding,AttentionViewModel>() {
    private lateinit var attentionAdapter: AttentionAdapter
    private val data:MutableList<AttentionEntity> = mutableListOf()
    private lateinit var linearLayoutManager: LinearLayoutManager

    private lateinit var gsyVideoHelper: GSYVideoHelper //帮助类支持小窗口播放
    private lateinit var gsyVideoHelperBuilder: GSYVideoHelper.GSYVideoHelperBuilder//视频播放器配置

    private var firstVisibleItem = 0
    private var lastVisibleItem = 0


    @RequiresApi(Build.VERSION_CODES.M)
    override fun initView() {

        initVideo()
        initRV()




    }
    private fun initVideo(){
        gsyVideoHelper = GSYVideoHelper(context)
        gsyVideoHelperBuilder = GSYVideoHelper.GSYVideoHelperBuilder()
        gsyVideoHelperBuilder.setHideStatusBar(true)
            .setHideActionBar(true)
            .setNeedLockFull(true)
            .setCacheWithPlay(true)
            .setShowFullAnimation(true)
            .setRotateViewAuto(false)
            .setLockLand(true)
            .setVideoAllCallBack(object : GSYSampleCallBack() {
                override fun onQuitSmallWidget(url: String?, vararg objects: Any?) {
                    super.onQuitSmallWidget(url, *objects)
                    //大于0说明有播放,//对应的播放列表TAG
                    //大于0说明有播放,//对应的播放列表TAG
                    if (gsyVideoHelper.playPosition >= 0 && gsyVideoHelper.playTAG == AttentionAdapter.TAG) {
                        //当前播放的位置
                        val position: Int = gsyVideoHelper.getPlayPosition()
                        //不可视的是时候
                        if (position < firstVisibleItem || position > lastVisibleItem) {
                            //释放掉视频
                            Log.d("aaa", "释放资源: ")
                            gsyVideoHelper.releaseVideoPlayer()
                            attentionAdapter.notifyDataSetChanged()
                        }
                    }
                }

            })
        gsyVideoHelper.setGsyVideoOptionBuilder(gsyVideoHelperBuilder)







    }


    @RequiresApi(Build.VERSION_CODES.M)
    private fun initRV(){
        binding?.let {
            attentionAdapter = AttentionAdapter(gsyVideoHelper,gsyVideoHelperBuilder,R.layout.community_item_attention_card_view,data)
            it.rv.adapter = attentionAdapter
            linearLayoutManager = LinearLayoutManager(context)
            it.rv.layoutManager = linearLayoutManager
            it.rv.setHasFixedSize(true)

            //滑动列表，判断是否显示小窗口
            it.rv.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
                firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition() //可见第一个下标
                lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition()//可见最后一个下标
                val playPosition = gsyVideoHelper.playPosition //正在播放的下标
                LoggerUtils.d(TAG,"findFirstVisibleItemPosition:$firstVisibleItem")
                LoggerUtils.d(TAG,"findLastVisibleItemPosition:$lastVisibleItem")
                LoggerUtils.d(TAG,"playPosition:$playPosition")
                //有播放而且tag正确
                if(playPosition >= 0 && gsyVideoHelper.playTAG.equals(AttentionAdapter.TAG)){
                    //不可见:显示小窗
                    if(playPosition < firstVisibleItem || playPosition > lastVisibleItem){
                        if(!gsyVideoHelper.isSmall){
                            gsyVideoHelper.showSmallVideo(Point(DensityUtils.dp2px(context,150f),DensityUtils.dp2px(context,100f)),true,true)
                        }
                        //可见：关闭小窗
                    }else{
                        if(gsyVideoHelper.isSmall){
                            gsyVideoHelper.smallVideoToNormal()
                        }
                    }
                }
            }
        }
    }

    override fun initData() {
        viewModel.attention().observe(this){
            LoggerUtils.d(TAG,it.toString())
            LoggerUtils.d(TAG,"$it.code")
            //追加数据
            attentionAdapter.addData(it.itemList)

        }

    }

    override fun initListener() {
        attentionAdapter.addChildClickViewIds(R.id.list_item_btn)
        attentionAdapter.setOnItemChildClickListener(object :OnItemChildClickListener{
            override fun onItemChildClick(
                adapter: BaseQuickAdapter<*, *>,
                view: View,
                position: Int,
            ) {
                gsyVideoHelper.setPlayPositionAndTag(position,
                    AttentionAdapter.TAG)
                gsyVideoHelperBuilder.setVideoTitle(attentionAdapter.data[position].data.content.data.title)
                    .setUrl(attentionAdapter.data[position].data.content.data.playUrl)

                gsyVideoHelper.startPlay()
            }

        })
    }

    override fun bindLayout(): Int {
        return R.layout.community_fragment_attention
    }

    override fun getJVMClass(): Class<AttentionViewModel> {
       return AttentionViewModel::class.java
    }

    companion object{
        const val TAG = "AttentionFragment"
    }

    override fun onDestroyView() {
        super.onDestroyView()
        gsyVideoHelper.releaseVideoPlayer()
        GSYVideoManager.releaseAllVideos()
    }

    override fun onPause() {
        GSYVideoManager.onPause()
        super.onPause()
    }

    override fun onResume() {
        GSYVideoManager.onResume()
        super.onResume()
    }


}